<?php
namespace Home\Controller;
use Think\Controller;
use Common\Model\ShopModel;
use Common\Model\UserMoneyModel;
use Common\Model\CurrencyModel;
use Common\Model\ShopBuyModel;
class ShopController extends HomeController {
    private $pay_currency_id;
    private $pay_currency;
    public function __construct()
    {
        parent::__construct();
        $this->pay_currency_id=8;
        $this->pay_currency=CurrencyModel::newSelf()->findOneById($this->pay_currency_id);
        $this->assign('pay_currency',$this->pay_currency);
    }
    public function index()
    {

        $user_money=UserMoneyModel::newSelf()->getUserMoneyInfo($this->home_user_info['id']);
        $this->assign('user_money',$user_money);
        $list=ShopModel::newSelf()->pagingList();
        $this->assign('list',$list);
        $this->display();
    }
  
    public function addShoppingCart()
    {
        $id=I('id');
        $this->notEmptyValidate($id,'请选择商品');
        $shop=ShopModel::newSelf()->findOneById($id);
        $this->notEmptyValidate($shop,'请选择商品');
       
        if ($_SESSION['cart'][$id]) {
            $_SESSION['cart'][$id]['buy_num']=1+$_SESSION['cart'][$id]['buy_num'];
        }else {
            $_SESSION['cart'][$id]=$shop;
            $_SESSION['cart'][$id]['buy_num']=1;
        }
        $this->returnResult('加入购物车成功',true);
    }
  
    public function delShoppingCart()
    {
        $id=I('id');
        $this->notEmptyValidate($id,'请选择商品');
        $shop=ShopModel::newSelf()->findOneById($id);
        $this->notEmptyValidate($shop,'请选择商品');
       
        unset($_SESSION['cart'][$id]);
        
        $this->returnResult('删除成功',true);
    }
    public function shoppingCart()
    {
        $user_money=UserMoneyModel::newSelf()->getUserMoneyInfo($this->home_user_info['id']);
        $this->assign('user_money',$user_money);
        $this->display();
    }
    public function payShopping()
    {
        $id=I('id');
        $this->notEmptyValidate($id,'请先选择商品');
        $data=[];
        $shop_model=ShopModel::newSelf();
        $all_money=0;
        foreach ($id as $k=>$v)
        {
            $shop=$shop_model->findOneById($v);
            if (!$shop) {
                continue;
            }
            $data[$v]['num']=(int)I('num'.$v);
            if (!$data[$v]['num']) {
                unset($data[$v]);
                continue;
            }
            if ($data[$v]['num']<=0) {
                unset($data[$v]);
                continue;
            }
            $all_money+=$data[$v]['num']*$shop['money'];
            $data[$v]['user_id']=$this->home_user_info['id'];
            $data[$v]['add_time']=time();
            $data[$v]['shop_id']=$shop['id'];
            $data[$v]['price']=$shop['money'];
            $data[$v]['all_price']=$data[$v]['num']*$shop['money'];
        }
        $money_model=UserMoneyModel::newSelf();
        
        $user_money=$money_model->getUserMoneyInfo($this->home_user_info['id']);
        $currency=CurrencyModel::newSelf()->getInfo();
        $this->egtValidate($user_money[$this->pay_currency['id']]['num'],$all_money,'您的'.$this->pay_currency['name'].'不足'.$all_money);
        $this->egtValidate($user_money[10]['num'],$all_money,'您的'.$currency[10]['name'].'不足'.$all_money);
        
        $res=$money_model->decUserMoney($this->home_user_info['id'], $this->pay_currency_id, $all_money);
        $res=$money_model->decUserMoney($this->home_user_info['id'], 10, $all_money);
        
        $buy_model=ShopBuyModel::newSelf();
        foreach ($data as $k=>$v)
        {
            $buy_model->add($v);
            unset($_SESSION['cart'][$v['shop_id']]);
        }

        $this->resReturn($res, '购买成功', '系统繁忙请稍后再试');
    }
    public function shopBuyLog()
    {
        $where['user_id']=$_SESSION['USER_KEY_ID'];

        $list=ShopBuyModel::newSelf()->pagingList($where);
        
        $this->assign('list',$list);
        $this->display();
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}